package com.lsz.custom2;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import com.lsz.custom2.activity.BrowserActivity;
import com.lsz.errorupload.DefaultExceptionHandler;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by leishize on 2016/10/19.
 */
public class DTBrowser extends CordovaPlugin {

    public static final int BROWSER = 1;

    public static CallbackContext callback;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        callback = callbackContext;
        JSONObject obj = args.getJSONObject(0);
        String url = obj.getString("url");
        boolean hasH5Data = false;
        try {
            hasH5Data = obj.getBoolean("hasH5Data");
        } catch (Exception e) {
            hasH5Data = false;
        }
        Intent it = new Intent(cordova.getActivity(), BrowserActivity.class);
        it.putExtra("url", url);
        it.putExtra("hasH5Data",hasH5Data);
        this.cordova.startActivityForResult(this, it, BROWSER);
        cordova.getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == BROWSER) {
            switch (resultCode) {
                case BrowserActivity.CLOSE:
                    String obj = intent.getStringExtra("jsonObj");
                    callback.success(obj);
                    break;
                case BrowserActivity.GODETAIL:
                    String obj1 = intent.getStringExtra("jsonObj");
                    callback.success(obj1);
                    break;
            }
        }
    }

}
